package com.mftour.common.customLabel;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.mftour.common.Constant;
import com.mftour.common.cache.InitCache;
import com.mftour.moudles.entity.ConsolePermission;
import com.mftour.moudles.entity.User;

public class MenuTag extends TagSupport {

	private List<ConsolePermission> userMenuPermissions;

	public List<ConsolePermission> getUserMenuPermissions() {
		return userMenuPermissions;
	}

	public void setUserMenuPermissions(List<ConsolePermission> userMenuPermissions) {
		this.userMenuPermissions = userMenuPermissions;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -9166304126306005512L;

	private int firstPage = 1;

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		firstPage = 1;

		User loginUser = (User) request.getSession().getAttribute(Constant.SESSION_LOGIN_USER);
		userMenuPermissions = InitCache.allUserMenuPermissions.get(loginUser.getUserId());
		StringBuilder sb = new StringBuilder();
		if (userMenuPermissions != null) {
			for (ConsolePermission menuPermission : userMenuPermissions) {
				sb.append("<li class='treeview'>");
				if (menuPermission.getPermissionType().equals("0")) {
					sb.append("<a href='#'>");
				} else {
					sb.append("<a href=\"#\" onclick=\"myMenuSubmit(this,'','").append(menuPermission.getPermissionUrl()).append("','").append(menuPermission.getPermissionCode()).append("','")
							.append(menuPermission.getPermissionDesc()).append("','").append(menuPermission.getPermissionName()).append("')\"");
					if (firstPage == 1) {
						sb.append(" id=\"firstPage\" ");
					}
					firstPage = firstPage + 1;
					sb.append(">");
				}
				sb.append(menuPermission.getPermissionIcon()).append(" <span>");
				sb.append(menuPermission.getPermissionName()).append("</span>");
				if (menuPermission.getPermissionType().equals("0")) {
					sb.append("<i class='fa fa-angle-left pull-right'></i>");
				}
				sb.append("</a> ");
				if (menuPermission.getPermissionType().equals("0")) {
					sb.append(createMenu(menuPermission, menuPermission.getPermissionName()));
				}
				sb.append("</li>");
			}
		}
		try {
			pageContext.getOut().write(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return super.doStartTag();
	}

	private String createMenu(ConsolePermission menuPermissionDetail, String title) {
		StringBuilder sb = new StringBuilder();
		sb.append("<ul class='treeview-menu'>");
		for (ConsolePermission menuPermission : menuPermissionDetail.getPermissions()) {
			sb.append("<li class='treeview'>");
			if (menuPermission.getPermissionType().equals("0")) {
				sb.append("<a href='#'>");
			} else {
				sb.append("<a href=\"#\" onclick=\"myMenuSubmit(this,'','").append(menuPermission.getPermissionUrl()).append("','").append(menuPermission.getPermissionCode()).append("','")
						.append(menuPermission.getPermissionDesc()).append("','").append(menuPermission.getPermissionName()).append("','").append(title + "')\"");
				if (firstPage == 1) {
					sb.append(" id=\"firstPage\" ");
				}
				firstPage = firstPage + 1;
				sb.append(">");
			}
			sb.append(menuPermission.getPermissionIcon()).append(" <span>");
			sb.append(menuPermission.getPermissionName()).append("</span>");
			if (menuPermission.getPermissionType().equals("0")) {
				sb.append("<i class='fa fa-angle-left pull-right'></i>");
			}
			sb.append("</a>");
			if (menuPermission.getPermissionType().equals("0")) {
				sb.append(createMenu(menuPermission, title + "," + menuPermission.getPermissionName()));
			}
			sb.append("</li>");
		}
		sb.append("</ul>");
		return sb.toString();
	}

	@Override
	public void release() {
		super.release();
	}

}
